﻿using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using DataGridViewAutoFilter;
using ProrabHelp.Library;

namespace ProrabHelp.UI
{
    public partial class Products : Form
    {
        bool mSelectVisible = false;
        ServerObject mServerObject = null;
        bool mNewRecord = false;

        public bool SelectButtonVisible
        {
            get { return mSelectVisible; }
            set { mSelectVisible = value; }
        }


        public Products()
        {
            InitializeComponent();
            mServerObject = new ServerObject();
        }

        private void FillMeasureUnitComboBox()
        {
            DataTable vTable = mServerObject.GetClassifiersListByTypeID(UFT.Table_TypeID.NAME_MeasureUnit);
            measureUnitComboBox.DataSource = vTable;
            measureUnitComboBox.DisplayMember = "Name";
            measureUnitComboBox.ValueMember = "CodeID";
        }

        private void FillProductsGridView()
        {
            BindingSource bindigSource = new BindingSource(mServerObject.GetProductList(), null);
            productsGridView.DataSource = bindigSource;

            if (productsGridView.DataSource != null)
            {
                foreach (DataGridViewColumn col in productsGridView.Columns)
                {
                    col.HeaderCell = new DataGridViewAutoFilterColumnHeaderCell(col.HeaderCell);
                }

                productsGridView.AutoResizeColumns();
            }
        }

        private void Products_Load(object sender, EventArgs e)
        {
            FillMeasureUnitComboBox();
            FillProductsGridView();

            tableLayoutPanel1.SetColumnSpan(productsGridView, 6);
            tableLayoutPanel2.Visible = false;
            selectButton.Visible = mSelectVisible;
            infoLabel.Text = string.Empty;
        }

        private void newButton_Click(object sender, EventArgs e)
        {
            tableLayoutPanel1.SetColumnSpan(productsGridView, 5);
            tableLayoutPanel2.Visible = true;

            mNewRecord = true;
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            tableLayoutPanel1.SetColumnSpan(productsGridView, 6);
            tableLayoutPanel2.Visible = false;

            mNewRecord = false;
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            tableLayoutPanel1.SetColumnSpan(productsGridView, 6);
            tableLayoutPanel2.Visible = false;
            bool resultOperation = false;
            if (mNewRecord)
            {
                resultOperation = mServerObject.AddProduct((int)measureUnitComboBox.SelectedValue, descriptionTextBox.Text.Trim());
            }
            else
            {
                int pructID = 0;
                if (productsGridView.SelectedRows.Count == 1)
                {
                    int.TryParse(productsGridView.Rows[productsGridView.SelectedRows[0].Index].Cells["id"].Value.ToString(), out pructID);
                }

                if (pructID != 0)
                    resultOperation = mServerObject.UpdateProduct(pructID, (int)measureUnitComboBox.SelectedValue, descriptionTextBox.Text.Trim());
            }

            if (resultOperation)
            {
                infoLabel.Text = "Data saved Successful!";
            }
            else
            {
                infoLabel.Text = "Failure savind data!";
            }

            FillProductsGridView();
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void editButton_Click(object sender, EventArgs e)
        {
            tableLayoutPanel1.SetColumnSpan(productsGridView, 5);
            tableLayoutPanel2.Visible = true;

            if (productsGridView.SelectedRows.Count == 1)
            {
                int productID = 0;
                int.TryParse(productsGridView.Rows[productsGridView.SelectedRows[0].Index].Cells["id"].Value.ToString(), out productID);
                measureUnitComboBox.SelectedValue = productID;
                descriptionTextBox.Text = productsGridView.Rows[productsGridView.SelectedRows[0].Index].Cells["Description"].Value.ToString();
            }
        }

        private void refreshButton_Click(object sender, EventArgs e)
        {
            FillProductsGridView();
        }

        private void productsGridView_SelectionChanged(object sender, EventArgs e)
        {
            infoLabel.Text = string.Empty;
        }

        private void deleteButton_Click(object sender, EventArgs e)
        {

        }
    }
}
